package com.fare.taluo.action.system.register;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.dayatang.domain.InstanceFactory;
import com.fare.taluo.application.plane.channel.SourceApplication;
import com.fare.taluo.application.system.email.ReportEmailApplication;
import com.fare.taluo.application.system.register.RegisterCodeSendApplication;
import com.fare.taluo.base.struts2.json.BaseJsonStrutsAction;
import com.fare.taluo.base.struts2.json.JsonReturnBean;
import com.fare.taluo.core.domain.queue.message.QueueRegisterEmailInfoEntity;
import com.fare.taluo.util.I18nUtil;
import com.fare.taluo.util.param.ParamUtils;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.reportemail.EmailVO;

@Controller("sendCodeRequestAction")
@Namespace("/register/sendcoderequest")
public class SendCodeRequestAction extends BaseJsonStrutsAction {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1556243888195186090L;
	private List<EmailVO> emailList;
	private List<SourceEntityVO> sourceList;
	@Resource(name ="sourceApplication")
	private SourceApplication sourceApplication;
	
	@Resource(name ="reportEmailApplication")
	private ReportEmailApplication reportEmailApplication;
	
	@Action(value="request",
			results={@Result(name="success", type="json", params={"root","returnBean"})})
	public String request() throws Exception {
		String email = ParamUtils.getParameter(request, "emailAdress");
		String company = ParamUtils.getParameter(request, "company");
		String body = ParamUtils.getParameter(request, "emailBody");
		returnBean = new JsonReturnBean();
		try {
			SourceEntityVO sourceVo = sourceApplication.findByHbaseKey(company);

				emailList = reportEmailApplication.findAllEmail(1);// 0表示不接受状态，1表示接受状态
				StringBuffer emailStr = new StringBuffer();
				for (EmailVO emailVo : emailList) {
					emailStr.append("," + emailVo.getEmailAddress());
				}
				if (emailStr.length() > 1 && emailStr != null) {
					email = emailStr.toString().substring(1);
				}
				String title = sourceVo.getName()+"请求邀请码";
				QueueRegisterEmailInfoEntity queueEmail = new QueueRegisterEmailInfoEntity();
				if(StringUtils.isNotBlank(email)){
					queueEmail.setBody("<p>公司名称："+sourceVo.getName()+"</p><p>&nbsp;&nbsp;&nbsp;Email："+email+"</p><p>&nbsp;&nbsp;&nbsp;&nbsp;正文："+body+"</p>");//正文
					queueEmail.setSubject(title);//主题
					queueEmail.setToAddress(email);
					queueEmail.setName(title);
					InstanceFactory.getInstance(RegisterCodeSendApplication.class).save(queueEmail);
				}
				returnBean.setHasOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			returnBean.setHasOk(false);
			returnBean.setMessage(e.getMessage());
		}
		return SUCCESS;
	}
	
	@Action(value="view",
			results={@Result(name="success", type="dispatcher", location="/pages/system/register/send_requestcode_email.jsp")})
	public String view() throws Exception{
		sourceList = sourceApplication.findCrawlAirCompanys();
		I18nUtil.sourceConvert2I18N(sourceList, super.getLocale());
		return SUCCESS;
	}


	public List<SourceEntityVO> getSourceList() {
		return sourceList;
	}

	public void setSourceList(List<SourceEntityVO> sourceList) {
		this.sourceList = sourceList;
	}
}
